package com.example.administrator.kotlin.activity

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import com.example.administrator.kotlin.R
import com.example.administrator.kotlin.adapter.StaggerAdapter
import com.example.administrator.kotlin.bean.NewsBean
import com.example.administrator.kotlin.bean.Newslist
import com.google.gson.Gson
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import com.orhanobut.logger.Logger

class StaggerActivity : AppCompatActivity() {
    private lateinit var recyclerview: RecyclerView
    private lateinit var adater: StaggerAdapter
    private val newslist: ArrayList<Newslist>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_stagger)
        initView()
        initData()

    }

    private fun initData() {
        OkGo.get<String>(url)
                .tag("stagger")
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        var gson: Gson = Gson()
                        val newslist1 = gson.fromJson(response?.body().toString(), NewsBean::class.java)
                        adater.setNewData(newslist1.newslist)

                    }

                    override fun onError(response: Response<String>?) {
                        Logger.i(response?.body().toString())
                    }

                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {

                    }

                    override fun onFinish() {

                    }


                })
    }

    private fun initView() {
        recyclerview = findViewById<RecyclerView>(R.id.recyclerview_stagger)
        recyclerview.layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        recyclerview.setHasFixedSize(true)
        adater = StaggerAdapter(R.layout.item_image, newslist)
        recyclerview.adapter = adater

    }

    companion object {
        private val url = "http://api.tianapi.com/social/?key=861a794733dd2b057e415d822bef9586&num=20"
    }
}
